﻿#coding:utf-8

#Import Modules
import pygame
from pygame.locals import *

class BoxControls(pygame.sprite.Sprite):
	'''
		Esta classe será a responsável por posicionar os VisualProperty em uma Surface, que então será
		desenhada na tela do jogo. 
			1. VisualProperty são adicionadas.
			2. create_box_control() cria a superfície e posiciona os VisualPropery nela
			
		Só será implementado uma função de posicionamento de propriedades: UmPorLinha()
	'''
	
	padding_top = 10
	margin_left = 10
	
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		
		self.image = None
		self.rect = None
		
		self.clear_surf = None
		
		self.vpdict = {}
		
	def add_visual_property(self,vp):
		vp.box_control = self		
		self.vpdict.update( {vp.id:vp} )
		
	def create_box_control(self):
		img_w,img_h = 0,0
		
		for vp in self.vpdict.values():
			if vp.width > img_w:
				img_w = vp.width
				
			img_h += vp.height + BoxControls.padding_top
		
		img_w += BoxControls.margin_left
			
		self.image = pygame.Surface( (img_w,img_h) ).convert()				
		self.rect = self.image.get_rect()
		
		self.clear_surf = pygame.Surface( (img_w,img_h) ).convert()
		self.clear_surf.fill( (255,255,255) )
		
		
		self._vp_has_updated = True
		self.update()
		

	def vp_has_updated(self,vp):
		'''
			VP irão chamar este método quando forem atualizados. O dict é usado para que eu posso
			atualizar imediatamente a instância presente no BoxControl
		'''
		self._vp_has_updated = True
		
		if vp._setted:
			for v in self.vpdict.values():
				if v.id == vp.id:
					continue
					
				v.unset()
		
		self.vpdict.update( {vp.id:vp} )
			
	def update(self):
		'''
			Quando um VP for atualizado, este deve ser atualizado também
		'''

		if self._vp_has_updated:
			self._vp_has_updated = False

			self.clear()
								
			pos = [BoxControls.margin_left,BoxControls.padding_top]
			for vp in self.vpdict.values():	
				rect = vp.rect
				rect.x = pos[0]
				rect.y = pos[1]
				
				pygame.Surface.blit( self.image, vp.image, rect )			
				
				pos[1]+= vp.height + BoxControls.padding_top
			
	def clear(self):		
		pygame.Surface.blit( self.image, self.clear_surf,self.clear_surf.get_rect() )
		
		
		
class VisualProperty():
	_id_counter = 0
	
	def __init__(self,name,inc,max=10000):
		self._setted = False
		
		self.id = VisualProperty._id_counter
		self.name = name
		self.inc = inc
		self.max = max
		
		self.image = None
		self.rect = None
		
		self.width = 0
		self.height = 0
		
		self._has_changed = False
		
		self.box_control = None #BoxControl a qual ele pertence
		
		VisualProperty._id_counter+=1
		
	def has_changed(self):
		ret = self._has_changed
		self._has_changed = False
		
		return ret
		
	def render_vp(self):
		'''
			Renderiza o VP. Rotina interna.
		'''
		if pygame.font:
			font = pygame.font.Font(None, 18)
			name_text_rendered = font.render(self.name + ":  ", 1, (0, 0, 0))
			value_text_rendered = font.render(self.value, 1, (0, 0, 0))
									
			self.width = font.size(self.name + ":  " + self.value)[0]*1.1
			self.height = font.size(self.name+":  ")[1]
			
		self.image = pygame.Surface( (self.width,self.height) ).convert()
		
		if self._setted:
			self.image.fill( (255,255,0) )
		else:
			self.image.fill( (255,255,255) )
			
		self.rect = self.image.get_rect()
			
		rect_name = name_text_rendered.get_rect()
		rect_name.x = 0
		rect_name.y = 0
		
		pygame.Surface.blit( self.image,name_text_rendered,rect_name)
		
		rect_value = value_text_rendered.get_rect()
		rect_value.x = name_text_rendered.get_width()
		rect_value.y = 0
		
		pygame.Surface.blit( self.image,value_text_rendered,rect_value)		
		
		if self.box_control is not None:
			self.box_control.vp_has_updated(self)								
						
					
	def set_value(self,value):
		self.value = value
		self.render_vp()
		
		self._has_changed = True

			
	def set(self):
		self._setted = True
		self.render_vp()
		
		
	def unset(self):
		self._setted = False
		self.render_vp()
		
			
	def get_value(self):
		if type( self.inc ) == type(0.1):
			return float(self.value)
		else:
			return int(self.value)
			
			
		
		
		
		
		
		
		